<?php
namespace app\resource\controller;
use app\common\controller\HomeCommon;
use think\Request;

/**
 * 
 * 行业资源
 *
 */
class Industry extends HomeCommon
{
    public function index()
    {
    }
    /**
     * 行业列表
     */
    public function getlists(){
    	if(Request::instance()->isPost()){
    		$keyword=input('keyword');
    	
    		
    		
    		$where=" 1=1 and status=1 ";
    		/* if($keyword){
    			$where.=" and name like '%$keyword%'";
    		} */
    		$list=db('industry')->field('id,name,pid')->where($where)->order('id desc')->select();
    		$result=two_dimensional_array($list,'pid');
    		
    		if($keyword){
    			$return=[];
    			$i=0;
	    		foreach ($result as $key=>$val){
	    			if(strstr($val['name'],$keyword)){
	    				$return[$i]=$val;
	    			}else{
	    				foreach ($val['children'] as $key1=>$val1){
	    					if(strstr($val1['name'],$keyword)){
	    						$return[$i]=['id'=>$val['id'],'name'=>$val['name'],'pid'=>$val['pid']];
	    						$return[$i]['children'][]=$val1;
	    					}
	    				}
	    			}
	    			$i++;
	    		}
    		}else{
    			$return=$result;
    		}
    		return json(['errcode'=>'0','msg'=>'获取成功！','data'=>$return]);
    	}
    }
    /**
     * 添加行业
     */
    public function addindustry(){
    	if(Request::instance()->isPost()){
    		$content=input('content');
    		if(empty($content)){
    			return json(['errcode'=>'10001','msg'=>'内容不能为空！','data'=>[]]);
    		}else{
    			
    			//判断行业是否存在
    			$info=db('industry')->field('id,name')->where(['name'=>$content,'status'=>'1'])->find();
    			if(!empty($info)){
    				return json(['errcode'=>'10002','msg'=>'行业已存在！','data'=>$info]);
    			}
    		}
    		$data['name']=$content;
    		$data['status']=0;
    		$result=db('industry')->insertGetId($data);
    		if($result){
    			$record['industry_id']=$result;
    			$token=input('token');
    			
    			$result=$this->check_token($token);
    			
    			if($result['errcode']!=0){
    				$record['user_id']=$this->user_id;
    			}
    			$record['status']=1;
    			
    			$result=db('industry_addrecord')->insert($record);
    			
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[['id'=>db('industry_addrecord')->getLastInsID(),'name'=>$content]]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    	}
    }
    
   
}
